Skip to contentMethod: tripleRange(Pair, int, int)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import javax.annotation.concurrent.Immutable;
32: import java.util.stream.IntStream;
33: import java.util.stream.Stream;
34: import lombok.EqualsAndHashCode;
35: import lombok.Getter;
36: import lombok.RequiredArgsConstructor;
37: import lombok.ToString;
38:
39: /***********************************************************************************************************************
40: *
41: * A value object that contains a triple of values.
42: *
43: * @author Fabrizio Giudici
44: * @since 3.2-ALPHA-12
45: * @it.tidalwave.javadoc.draft
46: *
47: **********************************************************************************************************************/
48: @Immutable @RequiredArgsConstructor(staticName = "of") @ToString @EqualsAndHashCode
49: public class Triple<A, B, C>
50: {
51: @Getter @Nonnull
52: public final A a;
53:
54: @Getter @Nonnull
55: public final B b;
56:
57: @Getter @Nonnull
58: public final C c;
59:
60: /*******************************************************************************************************************
61: *
62: * Creates a {@code Triple} from a {@code Pair} and another value.
63: *
64: * @param <T> the former type of the {@code Pair}
65: * @param <U> the latter type of the {@code Pair}
66: * @param <V> the type of the value
67: * @param pair the {@code Pair}
68: * @param value the value
69: * @return the {@code Stream} of {@code Triple}s
70: *
71: ******************************************************************************************************************/
72: @Nonnull
73: public static <T, U, V> Triple<T, U, V> of (@Nonnull final Pair<T, U> pair, @Nonnull final V value)
74: {
75: return of(pair.a, pair.b, value);
76: }
77:
78: /*******************************************************************************************************************
79: *
80: * Creates a {@link Stream} of {@code Triple}s composed of a given {@code Pair} and another element taken from
81: * {@link Stream}.
82: *
83: * @param <T> the former type of the {@code Pair}
84: * @param <U> the latter type of the {@code Pair}
85: * @param <V> the type of the {@code Stream}
86: * @param pair the {@code Pair}
87: * @param stream the {@code Stream}
88: * @return the {@code Stream} of {@code Triple}s
89: *
90: ******************************************************************************************************************/
91: @Nonnull
92: public static <T, U, V> Stream<Triple<T, U, V>> tripleStream (@Nonnull final Pair<T, U> pair,
93: @Nonnull final Stream<? extends V> stream)
94: {
95: return stream.map(value -> Triple.of(pair, value));
96: }
97:
98: /*******************************************************************************************************************
99: *
100: * Creates a {@link Stream} of {@code Triple}s composed of a given fixed {@code Pair} and an integer in the given
101: * range.
102: *
103: * @param <T> the former type of the {@code Pair}
104: * @param <U> the latter type of the {@code Pair}
105: * @param pair the {@code Pair}
106: * @param from the first value of the integer {@code Stream} (included)
107: * @param to the last value of the integer {@code Stream} (excluded)
108: * @return the {@code Stream} of {@code Triple}s
109: *
110: ******************************************************************************************************************/
111: @Nonnull
112: public static <T, U> Stream<Triple<T, U, Integer>> tripleRange (@Nonnull final Pair<T, U> pair,
113: @Nonnegative final int from,
114: @Nonnegative final int to)
115: {
116: return tripleStream(pair, IntStream.range(from, to).boxed());
117: }
118:
119: /*******************************************************************************************************************
120: *
121: * Creates a {@link Stream} of {@code Triple}s composed of a given fixed {@code Pair} and an integer in the given
122: * range.
123: *
124: * @param <T> the former type of the {@code Pair}
125: * @param <U> the latter type of the {@code Pair}
126: * @param pair the {@code Pair}
127: * @param from the first value of the integer {@code Stream} (included)
128: * @param to the last value of the integer {@code Stream} (included)
129: * @return the {@code Stream} of {@code Triple}s
130: *
131: ******************************************************************************************************************/
132: @Nonnull
133: public static <T, U> Stream<Triple<T, U, Integer>> tripleRangeClosed (@Nonnull final Pair<T, U> pair,
134: @Nonnegative final int from,
135: @Nonnegative final int to)
136: {
137: return tripleStream(pair, IntStream.rangeClosed(from, to).boxed());
138: }
139: }